package DAO;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import POJO.Agent;

@Repository
public class AgentDao {
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	public String getUserMsgTemplate(int agentId){
		String sql="SELECT agent_user_sms_template FROM agent where agent_id=?;";
		String messageTempalte=null;
		Object[] params=new Object[]{agentId};
		try {
			messageTempalte=jdbcTemplate.queryForObject(sql, params,String.class);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return messageTempalte;		
	}

	public String getCourierMsgTemplate(int agentId){
		String sql="SELECT agent_courier_sms_template FROM agent where agent_id=?;";
		String messageTempalte=null;
		Object[] params=new Object[]{agentId};
		try {
			messageTempalte=jdbcTemplate.queryForObject(sql, params,String.class);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return messageTempalte;		
	}

	
//	public String getFeedbackUrl(int agentId){
//		String sql="SELECT agent_feedback_url FROM agent where agent_id=?;";
//		String feedbackUrl=null;
//		Object[] params=new Object[]{agentId};
//		try {
//			feedbackUrl=jdbcTemplate.queryForObject(sql, params,String.class);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return feedbackUrl;		
//	}
	
	public Agent getAgentById(int agentId){
		String sql="SELECT * FROM agent where agent_id=?;";
		Object[] params=new Object[]{agentId};
		final Agent agent=new Agent();
		jdbcTemplate.query(sql, params,new RowCallbackHandler() {
			
			public void processRow(ResultSet rs) throws SQLException {
				agent.setId(rs.getInt("agent_id"));
				agent.setName(rs.getString("agent_name"));
				agent.setPassword(rs.getString("agent_pwd"));
				agent.setPartnerId(rs.getString("agent_partner_id"));
				agent.setFeedbackUrl(rs.getString("agent_feedback_url"));
			}
		});
		if(agent.getId()==-1){
			return null;
		}else{
			return agent;
		}
	}
	
	public String getPartnerName(int agentId){
		String sql="SELECT agent_partner_name FROM agent where agent_id=?;";
		String partnerName=null;
		Object[] params=new Object[]{agentId};
		try {
			partnerName=jdbcTemplate.queryForObject(sql, params,String.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return partnerName;		
	}	
	public Agent findByPartnerId(String partnerId){
		final Agent agent=new Agent();
		String sql="SELECT * FROM agent where agent_partner_id=?;";
		Object[] params=new Object[]{partnerId};
		jdbcTemplate.query(sql, params,new RowCallbackHandler() {
			
			public void processRow(ResultSet rs) throws SQLException {
				agent.setId(rs.getInt("agent_id"));
				agent.setName(rs.getString("agent_name"));
				agent.setPassword(rs.getString("agent_pwd"));
				agent.setPartnerId(rs.getString("agent_partner_id"));
			}
		});
		if(agent.getId()==-1){
			return null;
		}else{
			return agent;
		}
	}
}
